home *** CD-ROM | disk | FTP | other *** search
/ Amiga News 95 / Amiga News 95.iso / dpat / dpat31 / iobject / sources.lha / sources / Text.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-23  |  3.2 KB  |  165 lines

  1. // Routines pour l'affichage V0.16
  2. // (C) Christophe PASSUELLO
  3. // Sat Feb 20 15:18:58 1993
  4.  
  5.  
  6. #include <exec/types.h>
  7. #include <mytypes.h>
  8. #include <graphics/text.h>
  9. #include <graphics/rastport.h>
  10. #define  INTUITION_PREFERENCES_H 0
  11. #include "IObject_priv.h"
  12.  
  13. IMPORT __far CPTR DiskfontBase;        // Diskfont Library
  14. IMPORT UBYTE BackPen;
  15.  
  16. // Contantes pour le centrage
  17. #define VERT_DELTA 3
  18. #define HORIZ_DELTA 8
  19.  
  20. // Fonte par defaut
  21. const static struct TextAttr safe_font=
  22. {
  23.     "topaz.font",
  24.     8,
  25.     FS_NORMAL,
  26.     FPF_ROMFONT
  27. };
  28.  
  29.  
  30. //
  31. // Charge une fonte, si elle n'existe pas renvoie la Topaz 8
  32. //
  33. struct TextFont *LoadFont(struct TextAttr *Attribute)
  34. {
  35.     struct TextFont *Font;
  36.  
  37.     if (!Attribute)
  38.         Attribute = &safe_font;
  39.  
  40.     if (Font = OpenFont(Attribute))
  41.         return (Font);
  42.     else
  43.     {
  44.         // verifie que la Diskfont est ouverte
  45.         if (DiskfontBase)
  46.         {
  47.             if (Font = OpenDiskFont(Attribute))
  48.                 return (Font);
  49.         }
  50.         return(OpenFont(&safe_font));
  51.     }
  52. }
  53.  
  54.  
  55. //
  56. // Calcule la taille en pixels d'une chaine avec HotKey
  57. //
  58. UWORD MyTextWidth(struct RastPort *rp, STRPTR text, BOOL hotkey)
  59. {
  60.     STRPTR next;
  61.     UWORD size;
  62.  
  63.     size = 0;
  64.     if (hotkey)
  65.         {
  66.             size = TextLength(rp, text, next - text);
  67.             text = next + 1;
  68.         }
  69.     size += TextLength(rp, text, strlen(text));
  70.     return (size);
  71. }
  72.  
  73.  
  74. //
  75. // Affiche un texte avec un hotkey
  76. //
  77. VOID MyText(struct RastPort *rp, STRPTR text, BOOL hotkey)
  78. {
  79.     STRPTR next;
  80.  
  81.     if (hotkey)
  82.         if (next = strchr(text, '_'))
  83.         {
  84.             Text(rp, text, next - text);
  85.             SetSoftStyle(rp, FSF_UNDERLINED, -1);
  86.             text = next + 1;
  87.             Text(rp, text, 1);
  88.             SetSoftStyle(rp, FS_NORMAL, -1);
  89.             text++;
  90.         }
  91.     Text(rp, text, strlen(text));
  92. }
  93.  
  94.  
  95. //
  96. // Affiche un texte par rapport a une boite
  97. //
  98. VOID PrintLabelText(struct RastPort *rp, struct Box *box, STRPTR text, UWORD Flags)
  99. {
  100.     UWORD width;
  101.     WORD    dx, dy;
  102.  
  103.     width = MyTextWidth(rp, text, Flags & LABEL_HOT_KEY);
  104.  
  105.     // calcule la position du texte par rapport a la boite
  106.     if (Flags & (LABEL_ABOVE|LABEL_BELOW|LABEL_INSIDE))
  107.         dx = (box->w - width) / 2;
  108.  
  109.     if (Flags & (LABEL_RIGHT|LABEL_LEFT|LABEL_INSIDE))
  110.         dy = (box->h - rp->Font->tf_YSize) / 2;
  111.  
  112.     if (Flags & LABEL_ABOVE)
  113.         dy = -VERT_DELTA - rp->Font->tf_YSize;
  114.  
  115.     if (Flags & LABEL_BELOW)
  116.         dy = VERT_DELTA + box->h;
  117.  
  118.     if (Flags & LABEL_RIGHT)
  119.         dx = HORIZ_DELTA + box->w;
  120.  
  121.     if (Flags & LABEL_LEFT)
  122.         dx = -HORIZ_DELTA - width;
  123.  
  124.     SetDrMd(rp, JAM1);
  125.     Move(rp, box->x + dx, box->y + dy + rp->Font->tf_Baseline);
  126.     MyText(rp, text, Flags & LABEL_HOT_KEY);
  127. }
  128.  
  129.  
  130. //
  131. // Efface un label
  132. //
  133. VOID EraseLabelText(struct RastPort *rp, struct Box *box, STRPTR text, UWORD Flags)
  134. {
  135.     UWORD width;
  136.     UWORD    x, y;
  137.  
  138.     width = MyTextWidth(rp, text, Flags & LABEL_HOT_KEY);
  139.     x = box->x;
  140.     y = box->y;
  141.  
  142.     // calcule la position du texte par rapport a la boite
  143.     if (Flags & (LABEL_ABOVE|LABEL_BELOW|LABEL_INSIDE))
  144.         x += ((box->w - width) / 2);
  145.  
  146.     if (Flags & (LABEL_RIGHT|LABEL_LEFT|LABEL_INSIDE))
  147.         y += ((box->h - rp->Font->tf_YSize) / 2);
  148.  
  149.     if (Flags & LABEL_ABOVE)
  150.         y -= (VERT_DELTA + rp->Font->tf_YSize);
  151.  
  152.     if (Flags & LABEL_BELOW)
  153.         y += (VERT_DELTA + box->h);
  154.  
  155.     if (Flags & LABEL_RIGHT)
  156.         x += (HORIZ_DELTA + box->w);
  157.  
  158.     if (Flags & LABEL_LEFT)
  159.         x -= (HORIZ_DELTA + width);
  160.  
  161.     SetDrMd(rp, JAM1);
  162.     SetAPen(rp, BackPen);
  163.     RectFill(rp, x, y, x + width - 1, y + rp->Font->tf_YSize - 1);
  164. }
  165.